home *** CD-ROM | disk | FTP | other *** search
- /*
- This file or something like it should be used in code resources
- when malloc is used. malloc allocates blocks of memory within which
- it returns a pointer to the memory you request. When you call free, the
- blocks aren't disposed. This can be a problem in a code resource. Call
- FreeAllMallocMemory when you are done with the code resource and ready
- to exit. Since the runtime code has no way of knowing when you want
- or don't want the memory released, you are responsible to clean up
- after yourself. This code does nothing with memory you explicitly
- allocate with NewPtr or NewHandle. You need to keep track of that
- memory yourself and dispose of it when you are done.
-
- Please see the folder "Code Resource Examples:-CW Code Resource Info:
- Memory Problems in CRs:FreeAllC++Memory:" on CW7 for an example of how
- to dispose of memory allocated with new.
-
- The malloc function CW uses doesn't use a data structure similar to
- mempools (used with new) that allows for easy freeing of the created blocks.
- I wrote a couple of functions and a replacement for the actual memory
- allocation function that keeps track of each block. I also don't guarantee
- that this will work for all subsequent versions of CW. Although I see no
- reason why they would change how malloc works, only use this file if you
- are willing to test that it is doing what you want. To test it, you would
- need to look in the heap to see if it is actually disposing the blocks.
-
- This example has been tested with the ANSI 68K and PPC libraries.
-
- Mark Anderson
- metrowerks
- 12/16/94
- */
-
- #include <stdlib.h>
- #include "FreeAllMallocMemory.h"
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- void * _Getmem(size_t size);
- void PutInMallocList(Ptr p);
-
- #ifdef __cplusplus
- }
- #endif /* __cplusplus */
-
- typedef struct mallocLinkedList{
- Ptr list;
- struct mallocLinkedList * next;
- }mallocLinkedList;
-
- static mallocLinkedList * mallocList = NULL;
-
- /*****************************************************************/
- /* Purpose..: Makes a mallocLinkedList, one for each block created */
- /* by NewPtr in _GetMem */
- /*****************************************************************/
- void PutInMallocList(Ptr p)
- {
- mallocLinkedList * local = mallocList;
-
- if (local)
- {
- while (local->next)
- {
- local = local->next;
- }
- local->next = (mallocLinkedList*)NewPtr(sizeof(mallocLinkedList));
- local = local->next;
- local->next = NULL;
- local->list = p;
-
- }
- else // initialize
- {
- mallocList = (mallocLinkedList*)NewPtr(sizeof(mallocLinkedList));
- mallocList->next = NULL;
- mallocList->list = p;
- }
- }
-
- /****************************************************************/
- /* Purpose..: Disposes of memory created by malloc and disposes */
- /* of the linked list of those blocks. */
- /****************************************************************/
- void FreeAllMallocMemory( void )
- {
- mallocLinkedList * local = mallocList;
- mallocLinkedList * temp;
-
- while (local)
- {
- temp = local;
- DisposePtr(local->list);
- local = local->next;
- DisposePtr((Ptr)temp);
- }
- mallocList = NULL;
-
- }
-
- /****************************************************************/
- /* Purpose..: Allocates memory for malloc. Replaces version in */
- /* ANSI library. */
- /****************************************************************/
- void *_Getmem(size_t size)
- {
- void *p;
- size_t isize = size;
- if (isize <= 0 || !(p = NewPtr(isize)))
- return 0L;
- PutInMallocList((Ptr)p);
- return p;
- }
-